<?php 


// UNTUK MENGGUNAKAN MODEL INI, SETIAP TABLE HARUS MEMILIKI
// id
// created_at
// updated_at
//pp 7-8 CodeIgniter Handbook Vol.1
class FM_Model extends CI_Model
{

	public function __construct()
	{
		parent::__construct();// p.9

		$this->load->database();
		//p.9 
		//We can now load CodeIgniter's inflector helper, 
		//which contains a bunch of useful functions for dealing with English language strings.
		$this->load->helper('inflector'); 

		if ( ! $this->_table)
		{
			$this->_table =
			strtolower((str_replace('_model', '',get_class($this))));
			//strtolower(plural(str_replace('_model', '',get_class($this))));
		}

	}

	/*
	public function get($where)
	{
		return $this->db->where($where)
		->get($this->_table)
		->row();
	}
*/

	//since Each table will contain an id column //p.11
	public function get()
	{
		$args = func_get_args();
		if (count($args) > 1 || is_array($args[0]))
		{
			$this->db->where($args);
		}
		else
		{
			$this->db->where('id', $args[0]);
		}
		return $this->db->get($this->_table)->row();
	}

	//select * from table
	public function all()
	{
		return $this->db->select()
		->get($this->_table)
		->result();

	}
	/*
	public function get_all2($where)
	{
		return $this->db->where($where)
		->get($this->_table)
		->result();
	}
*/
	//since Each table will contain an id column
	public function get_all()
	{
		$args = func_get_args();
		if (count($args) > 1 || is_array($args[0]))
		{
			$this->db->where($args);
		}
		else
		{
			$this->db->where('id', $args[0]);
		}
		return $this->db->get($this->_table)->result();
	}

	/*
	//insert without changing updated_at and created_at
	public function insert2($data)
	{
		return $this->db->insert($this->_table, $data);
	}
*/

	public function insert($data)
	{
		$data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s');
		$success = $this->db->insert($this->_table, $data);
		if ($success)
		{
			return $this->db->insert_id();
		}
		else
		{
			return FALSE;
		}
	}

	/*
	public function update2($where, $data)
	{
		return $this->db->where($where)
		->update($this->_table, $data);
	}
	public function delete2($where)
	{
		return $this->db->where($where)
		->delete($this->_table);
	}
*/
	/**
fungsi update yang ini dengan asumsi setiap tabel ada field id, dan updated_at
*/
	public function update()
	{
		$args = func_get_args();
		$args[1]['updated_at'] = date('Y-m-d H:i:s');//$args[1] should_be $data
		if (is_array($args[0]))
		{
			$this->db->where($args);
		}
		else
		{
			$this->db->where('id', $args[0]);
		}
		return $this->db->update($this->_table, $args[1]);
	}


	public function delete()
	{
		$args = func_get_args();
		if (count($args) > 1 || is_array($args[0]))
		{
			$this->db->where($args);
		}
		else
		{
			$this->db->where('id', $args[0]);
		}
		return $this->db->delete($this->_table);
	}

	//fungsi ini untuk memudahkan kita mengambil id dan nama saja
	//yang berguna untuk membuat select
	public function getIdNama($tabel){
		$this->db->select('id,nama');
		$result =  $this->db->get($tabel)->result();
		$result_ar = array();
		foreach ($result as $row){
			$result_ar[$row->id] = $row->nama;
		}
		return $result_ar;
	}

	/**
	* @input id id dari polygon
	* @return shape dari polygon tersebut
	*/
	public function getShape($id){
		$this->db->select('shape');
		$this->db->where('id',(int)$id);
		$result =  $this->db->get('polygon')->row();
		return $result->shape;
	}
	//return Polygon Option for selected $id
	public function getPolygonOptions($id){
		$this->db->select('strokeColor, strokeOpacity, strokeWeight, fillColor, fillOpacity');
		$this->db->where('id',(int)$id);
		return $this->db->get('polygon')->row();
	}

	/**get shape for spesified kelurahan
	* @input id id kelurahan
	* @return id_polygon dari kelurahan tersebut
	*/
	public function getKelurahanPolygonId($id){
		$this->db->select('id_polygon');
		$this->db->where('id',(int)$id);
		return $this->db->get('kelurahan')->row()->id_polygon;
	}
	
	public function getAllId($table){
		$this->db->select('id');
		return $this->db->get($table)->result();
	}
	
	protected $_table;

}
